﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// UnitMethodCreate 的摘要说明
/// </summary>
public class UnitMethodCreate
{
    #region 代码生成相关
    /// <summary>
    /// 用于替换的模板 如Response
    /// </summary>
    public string afnName = "#AFNNAME#";
    /// <summary>
    /// F1
    /// </summary>
    public string FN = "#FN#";
    /// <summary>
    /// (byte)InCom.Utilities.Protocol.AFN.RESPOND
    /// </summary>
    public string Afnstr = " #AFN#";
    /// <summary>
    /// 当前日期 如 2012-3-4
    /// </summary>
    public string date = "#Date#";
    /// <summary>
    /// AFN描述
    /// </summary>
    public string afnSummary = "#summary#";

    /// <summary>
    /// AFN服务描述
    /// </summary>
    public List<AFNSummary> summaryList = new List<AFNSummary>(){
    new AFNSummary(){  AFNValue =AFN.AUTHERTICATION ,AFNHEX= (( byte)AFN.AUTHERTICATION).ToString("X2"), AFNNAME="Authenticate", Summary="身份认证" },
    new AFNSummary(){AFNValue = AFN.CONTROL, AFNHEX= (( byte)AFN.CONTROL).ToString("X2"), AFNNAME="Control", Summary="控制协议" },
    new AFNSummary(){AFNValue =AFN.FILE_TRANSFER ,AFNHEX= (( byte)AFN.FILE_TRANSFER).ToString("X2"), AFNNAME="FileTransport", Summary="文件传输"},
    new AFNSummary(){AFNValue =AFN.PARAMETER_QUERY ,AFNHEX= (( byte)AFN.PARAMETER_QUERY).ToString("X2"), AFNNAME="ParamemterQuery", Summary="参数查询"},
    new AFNSummary(){AFNValue =AFN.PARAMETER_SETTINGS ,AFNHEX= (( byte)AFN.PARAMETER_SETTINGS).ToString("X2"), AFNNAME="ParameterSetting", Summary="参数设置"},
    new AFNSummary(){AFNValue =AFN.PASSTHROUGH ,AFNHEX= (( byte)AFN.PASSTHROUGH).ToString("X2"), AFNNAME="PassThrough", Summary="透传数据"},
    new AFNSummary(){AFNValue =AFN.REPORTED_DATA ,AFNHEX= (( byte)AFN.REPORTED_DATA).ToString("X2"), AFNNAME="RealtimeUpData", Summary="上报数据"},
    new AFNSummary(){AFNValue =AFN.RESPOND ,AFNHEX= (( byte)AFN.RESPOND).ToString("X2"), AFNNAME="Response", Summary="应答"},
    new AFNSummary(){ AFNValue=AFN.STATUS_QUERY,AFNHEX= (( byte)AFN.STATUS_QUERY).ToString("X2"), AFNNAME="QueryStatus", Summary="状态查询"},
    new AFNSummary(){ AFNValue=AFN.VEHICLE_FAULT,AFNHEX= (( byte)AFN.VEHICLE_FAULT).ToString("X2"), AFNNAME="VehicleFault", Summary="整车故障"},

    new AFNSummary(){ AFNValue=AFN.HISTORY_UP,AFNHEX= (( byte)AFN.HISTORY_UP).ToString("X2"), AFNNAME="HistoryDataReport", Summary="不丢包历史数据上报"},
    new AFNSummary(){ AFNValue=AFN.HISTORY_RET,AFNHEX= (( byte)AFN.HISTORY_RET).ToString("X2"), AFNNAME="HistoryDataResponse", Summary="不丢包历史数据上报回应"},
    new AFNSummary(){ AFNValue=AFN.REPORTED_DATA_NEWAFN23,AFNHEX= (( byte)AFN.REPORTED_DATA_NEWAFN23).ToString("X2"), AFNNAME="RealtimeUpDataAFN23", Summary="不丢包实时数据上报"},
    new AFNSummary(){ AFNValue=AFN.URLFile_Operation,AFNHEX= (( byte)AFN.URLFile_Operation).ToString("X2"), AFNNAME="UrlFileOperation", Summary="URL文件操作包括文件上传,下载主动上报进度主动上传等"},
    new AFNSummary(){ AFNValue=AFN.Terminal_State_Report,AFNHEX= (( byte)AFN.Terminal_State_Report).ToString("X2"), AFNNAME="TerminalStateReport", Summary="终端状态上报,用于终端主动上报终端当前状态"},

    //new AFNSummary(){},
    //new AFNSummary(){},
    
    };

    #endregion

    /// <summary>
    /// 生成数据
    /// </summary>
    /// <param name="txtAfn">AFN值 十六进制</param>
    /// <param name="txtFn">FN值,多个,分开</param>
    /// <param name="EntityTemplate">数据实体单元模板</param>
    /// <param name="EntityMethodTemplate">数据实体模板方法</param>
    public virtual void BulidCode(string txtAfn, string txtFn, string EntityTemplate, string EntityMethodTemplate)
    {
        if (string.IsNullOrEmpty(txtAfn) || string.IsNullOrEmpty(txtFn))
        {
            return;
        }
        //1.检查文件夹是否存在
        //2.创建文件夹
        //3.跟据AFN创建 文件
        //3.1 数据单元,
        //3.2 数据单元操作方法

        string codePath = HttpContext.Current.Server.MapPath("./Code/");
        //查询
        var afnque = summaryList.Where(p => p.AFNHEX == txtAfn.Trim().ToUpper());
        if (afnque.Count() > 0)
        {
            AFNSummary afnEntity = afnque.FirstOrDefault();
            string dir = codePath + "AFN" + afnEntity.AFNHEX;
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }
            //开始创建FN单元与FN的操作方法
            foreach (string fn in txtFn.Split(','))
            {
                string filenameUnit = "#AFNNAME#Entity#FN#";
                string filenameMethod = " #AFNNAME#Entity#FN#Method";

                #region 替换数据
                filenameUnit = filenameUnit
                                       .Replace("&lt;", "<")
                          .Replace("&gt;", ">")
                    .Replace(afnName, afnEntity.AFNNAME)
                         .Replace(FN, "F" + fn)
                         .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                         .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                         .Replace("afnSummary", afnEntity.Summary);
                filenameMethod = filenameMethod
                                       .Replace("&lt;", "<")
                                   .Replace("&gt;", ">")
                                .Replace(afnName, afnEntity.AFNNAME)
                                  .Replace(FN, "F" + fn)
                                  .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                                  .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                                  .Replace("afnSummary", afnEntity.Summary);
                #endregion


                string contentUnit = EntityTemplate
                                       .Replace("&lt;", "<")
                     .Replace("&gt;", ">")
                    .Replace(afnName, afnEntity.AFNNAME)
                    .Replace(FN, "F" + fn)
                    .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                    .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                    .Replace(afnSummary, afnEntity.Summary);

                string contentMethod = EntityMethodTemplate
                   .Replace("&lt;", "<")
                   .Replace("&gt;", ">")
                  .Replace(afnName, afnEntity.AFNNAME)
                  .Replace(FN, "F" + fn)
                  .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                  .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                  .Replace(afnSummary, afnEntity.Summary);


                //写文件
                System.IO.File.WriteAllText(dir + "/" + filenameUnit + ".cs", contentUnit);
                System.IO.File.WriteAllText(dir + "/" + filenameMethod + ".cs", contentMethod);


            }
        }
        else
        {
            HttpContext.Current.Response.Write("指定的AFN不存在");
        }



    }
}